package com.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicMain {
    public static void main(String[] args) {

        ExecutorService service = Executors.newCachedThreadPool();
        Count count = new Count();
        // 100个线程对共享变量进⾏加1
        for (int i = 0; i < 100; i++) {
            service.execute(() -> count.increase());
        }
        // 等待上述的线程执⾏完
        service.shutdown();
        try {
            service.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("公众号：Java3y---------");
        System.out.println(count.getCount());
    }
}

class Count{
    // 共享变量
    //private Integer count = 0;
    private AtomicInteger count = new AtomicInteger(0);
    public AtomicInteger getCount() {
        return count;
    }
    public  void increase() {
        count.incrementAndGet();
    }
   /* public synchronized void increase() {
        count++;
    }*/
}
